/**
 * File: WebConnector.java
 * Created: Feb 14, 2007 3:13:50 PM
 */
package com;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author Wes Billman
 * 
 */
public class WebConnector {
    private String m_proxyHost;

    private String m_proxyPort;

    private boolean m_proxyEnabled;

    public WebConnector(String proxyHost, String proxyPort, boolean proxyEnabled) {
	m_proxyHost = proxyHost;
	m_proxyPort = proxyPort;
	m_proxyEnabled = proxyEnabled;

	if (proxyEnabled) {
	    setupProxy();
	}
    }

    private void setupProxy() {
	System.getProperties().put("proxySet", "true");
	System.getProperties().put("proxyPort", m_proxyPort);
	System.getProperties().put("proxyHost", m_proxyHost);
    }

    public String getIndexFile(String file_name) {
	try {
	    URL fileURL = new URL(file_name);
	    InputStream in = fileURL.openStream();

	    byte[] buf = new byte[1024];
	    int len;
	    String output = null;
	    while ((len = in.read(buf)) > 0) {
		for (int i = 0; i < len; i++) {
		    output += (char) buf[i];
		}
	    }

	    in.close();
	    return output;
	} catch (MalformedURLException e) {
	    System.out.println("MalformedURL: " + e.getMessage());
	} catch (IOException e) {
	    System.out.println("File Not Found: " + e.getMessage());
	} finally {
	    // TODO
	}

	return "ERROR";
    }

    /**
         * @param args
         */
    public static void main(String[] args) {
	System.out.println("Create Conn");
	WebConnector conn = new WebConnector("192.232.119.194", "81", true);

	System.out.println("Get the file");
	conn.getIndexFile("http://www.wowace.com/files/descript.ion");
	System.out.println("Done");
    }

}
